package com.thinkgsp.webapp.controller;

import java.io.StringWriter;
import java.sql.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.thinkgsp.jxc.model.BillHistory;
import com.thinkgsp.jxc.service.BillHistoryService;

@Controller
@RequestMapping(value = "/billhis")
public class BillHistoryController extends BaseController {

	@Autowired
	private BillHistoryService billHistoryService;
	
	@RequestMapping(value="/recentsale.do")
	@ResponseBody
	public String getRecentSaleHistory(@RequestParam("custId")Integer custId,
			@RequestParam("productId")Integer productId) {
		List<BillHistory> list = billHistoryService.getRecentSaleHistory(custId, productId);
		return jsonWrite(list);
	}
	
	@RequestMapping(value="/recentpurchase.do")
	@ResponseBody	
	public String getRecentPurchaseHistory(Integer productId) {
		List<BillHistory> list = billHistoryService.getRecentPurchaseHistory(productId);
		return jsonWrite(list);
	}
	
	@RequestMapping(value="/salehistory.do")
	@ResponseBody	
	public Object getSaleHistory(@RequestParam("begDate")Date begDate,@RequestParam("endDate")Date endDate,
			@RequestParam("productId")Integer productId, @RequestParam("custId")Integer custId) {
		if(custId != null && custId.intValue() == 0) {
			custId = null;
		}
		return billHistoryService.getSaleHistory(begDate, endDate, productId, custId);
	}
	@RequestMapping(value="/purchasehistory.do")
	@ResponseBody
	public Object getPurchaseHistory(@RequestParam("begDate")Date begDate,@RequestParam("endDate")Date endDate,
			@RequestParam("productId")Integer productId, @RequestParam("custId")Integer custId) {
		if(custId != null && custId.intValue() == 0) {
			custId = null;
		}
		return billHistoryService.getPurchaseHistory(begDate, endDate, productId, custId);
	}
	
	private String jsonWrite(List<BillHistory> list) {
		if(list == null || list.size()==0) {
			return "[]";
		}
		JsonFactory factory = new JsonFactory();
		StringWriter out = new StringWriter();
		JsonGenerator g = null;
		try {
			g = factory.createJsonGenerator(out);
			g.writeStartArray();
			for(BillHistory bh : list) {
				g.writeStartObject();
				g.writeNumberField("id", bh.getId());
				g.writeStringField("billDate", bh.getBillDate().toString());
				if(bh.getCustName() != null) {
					g.writeStringField("custName", bh.getCustName());
				}
				g.writeStringField("batchNo", bh.getBatchNo());
				g.writeNumberField("quantity", bh.getQuantity());
				g.writeNumberField("price", bh.getPrice());
				g.writeNumberField("settlePrice", bh.getSettlePrice());
				g.writeEndObject();
			}
			g.writeEndArray();
			g.flush();
			out.flush();
			String json = out.toString();
			this.loger.debug(json);
			return json;
		} catch(Exception e) {
			try {
				out.close();
				if(g != null) {
					g.close();
				}
			} catch(Exception e1) {
				e.printStackTrace();
			}
			return "[]";
		}
	}
}
